<template>
  <div>
    <div v-if="query.roleId ===null">
      <div class="my-code">点击角色查看岗位成员信息</div>
    </div>
    <div v-else>
      <!--工具栏-->
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <el-input
            v-model="query.roleName"
            clearable
            size="small"
            placeholder="输入字典标签查询"
            style="width: 200px;"
            class="filter-item"
            @keyup.enter.native="toQuery"
          />
          <rrOperation />
        </div>
      </div>

      <!--表单组件-->
      <el-dialog
        append-to-body
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible="crud.status.cu > 0"
        :title="crud.status.title"
        width="550px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="用户" prop="userId">
            <el-input
              v-model="form.uname"
              style="width: 370px;"
              placeholder="点击选择用户"
              readonly
              @click.native="dialogFormVisible = true "
            />
          </el-form-item>
          <el-form-item label="部门" prop="orgId">
            <treeselect
              v-model="form.orgId"
              :options="depts"
              :load-options="loadDepts"
              style="width: 370px;"
              placeholder="选择部门"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <el-dialog title="用户选择" :visible.sync="dialogFormVisible" @open="$refs.userSelect && $refs.userSelect.crud.toQuery()&& $refs.userSelect.reset()">
        <user-select ref="userSelect" :permission="permission" @selected="selected" />
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        highlight-current-row
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column label="用户">
          <template slot-scope="scope">
            <div>{{ scope.row.uname }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="label" label="部门">
          <template slot-scope="scope">
            <div>{{ scope.row.oname }}</div>
          </template>
        </el-table-column>
        <el-table-column
          v-permission="['admin','role:edit','role:del']"
          label="操作"
          width="130px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import member from '@/api/system/member';
import CRUD, { presenter, header, form } from '@crud/crud';
import pagination from '@crud/Pagination';
import rrOperation from '@crud/RR.operation';
import udOperation from '@crud/UD.operation';
import { getDepts, getDeptSuperior } from '@/api/system/dept';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect';
import userSelect from '@/views/system/user/userSelect';

const defaultForm = { id: null, uname: null, orgId: null };
export default {
  name: 'Member',
  components: { pagination, rrOperation, udOperation, Treeselect, userSelect },
  cruds() {
    return [
      CRUD({
        title: '岗位成员详情',
        url: 'rest/api/member',
        query: { roleId: null },
        crudMethod: { ...member },
        optShow: {
          add: true,
          edit: true,
          del: true,
          reset: false
        },
        queryOnPresenterCreated: false
      })
    ];
  },
  mixins: [
    presenter(),
    header(),
    form(function() {
      return Object.assign({ roleId: this.roleId }, defaultForm);
    })],
  data() {
    return {
      roleId: null,
      depts: [],
      permission: {
        add: ['admin', 'role:add'],
        edit: ['admin', 'role:edit'],
        del: ['admin', 'role:del']
      },
      formLabelWidth: '120px',
      dialogTableVisible: false,
      dialogFormVisible: false,
      rules: {
        organization: {
          id: [{ required: true, message: '请选择部门', trigger: 'blur' }]
        }
      }
    };
  },
  methods: {
    // 获取弹窗内部门数据
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        getDepts({ enabled: true, pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null;
            }
            return obj;
          });
          setTimeout(() => {
            callback();
          }, 200);
        });
      }
    },
    buildDepts(depts) {
      depts.forEach(data => {
        if (data.children) {
          this.buildDepts(data.children);
        }
        if (data.hasChildren && !data.children) {
          data.children = null;
        }
      });
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.id == null) {
        this.getDepts();
      } else {
        console.log(form.orgId);
        this.getSupDepts(form.orgId);
      }
    },
    getSupDepts(deptId) {
      if (deptId !== null && undefined !== deptId) {
        getDeptSuperior(deptId).then(res => {
          const date = res.content;
          this.buildDepts(date);
          this.depts = date;
        });
      } else {
        this.getDepts();
      }
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null;
          }
          return obj;
        });
      });
    }, // 选中图标
    selected(data) {
      this.form.userId = data.id;
      this.form.uname = data.nickName;
    }
  }
};
</script>

<style scoped>

</style>
